﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace B2B.Models
{
    public class ThanhVien
    {
        int maTV;

        public int MaTV
        {
            get { return maTV; }
            set { maTV = value; }
        }

        string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        int maQuocGia;

        public int MaQuocGia
        {
            get { return maQuocGia; }
            set { maQuocGia = value; }
        }
        string tenQuocGia;

        public string TenQuocGia
        {
            get { return tenQuocGia; }
            set { tenQuocGia = value; }
        }
        int maTinhThanh_Bang;

        public int MaTinhThanh_Bang
        {
            get { return maTinhThanh_Bang; }
            set { maTinhThanh_Bang = value; }
        }
        string tenTinhThanh_Bang;

        public string TenTinhThanh_Bang
        {
            get { return tenTinhThanh_Bang; }
            set { tenTinhThanh_Bang = value; }
        }
        string hoTen;

        public string HoTen
        {
            get { return hoTen; }
            set { hoTen = value; }
        }
        string tenCongTy;

        public string TenCongTy
        {
            get { return tenCongTy; }
            set { tenCongTy = value; }
        }
        string soDienThoai;

        public string SoDienThoai
        {
            get { return soDienThoai; }
            set { soDienThoai = value; }
        }
        string diaChiCongTy;

        public string DiaChiCongTy
        {
            get { return diaChiCongTy; }
            set { diaChiCongTy = value; }
        }
        int namThanhLap;

        public int NamThanhLap
        {
            get { return namThanhLap; }
            set { namThanhLap = value; }
        }
        string website;

        public string Website
        {
            get { return website; }
            set { website = value; }
        }
        string quyMo;

        public string QuyMo
        {
            get { return quyMo; }
            set { quyMo = value; }
        }
        string diaChiNhaMay;

        public string DiaChiNhaMay
        {
            get { return diaChiNhaMay; }
            set { diaChiNhaMay = value; }
        }
        string quyMoNhaMay;

        public string QuyMoNhaMay
        {
            get { return quyMoNhaMay; }
            set { quyMoNhaMay = value; }
        }
        string hinhAnh;

        public string HinhAnh
        {
            get { return hinhAnh; }
            set { hinhAnh = value; }
        }
        string gioiThieuVeCongTy;

        public string GioiThieuVeCongTy
        {
            get { return gioiThieuVeCongTy; }
            set { gioiThieuVeCongTy = value; }
        }
        int maLoaiThanhVien;

        public int MaLoaiThanhVien
        {
            get { return maLoaiThanhVien; }
            set { maLoaiThanhVien = value; }
        }
        string tenLoaiThanhVien;

        public string TenLoaiThanhVien
        {
            get { return tenLoaiThanhVien; }
            set { tenLoaiThanhVien = value; }
        }
        int tinhTrangThanhVien;

        public int TinhTrangThanhVien
        {
            get { return tinhTrangThanhVien; }
            set { tinhTrangThanhVien = value; }
        }
        bool laThanhVien;

        public bool LaThanhVien
        {
            get { return laThanhVien; }
            set { laThanhVien = value; }
        }
        DateTime ngayBatDau;

        public DateTime NgayBatDau
        {
            get { return ngayBatDau; }
            set { ngayBatDau = value; }
        }
        DateTime ngayKetThuc;

        public DateTime NgayKetThuc
        {
            get { return ngayKetThuc; }
            set { ngayKetThuc = value; }
        }

        public ArrayList DanhSachThanhVien(int page)
        {
            ArrayList list_ThanhVien = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHTHANHVIEN", conn);
            cmd.Parameters.AddWithValue("@PAGECURENT", page);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ThanhVien tv = new ThanhVien();
                tv.MaTV = reader.GetInt32(0);
                tv.Email = reader.GetString(1);
                tv.Password = reader.GetString(2);
                tv.MaQuocGia = reader.GetInt32(3);
                tv.TenQuocGia = reader.GetString(4);
                tv.maTinhThanh_Bang = reader.GetInt32(5);
                tv.TenTinhThanh_Bang = reader.GetString(6);
                tv.HoTen = reader.GetString(7);
                tv.TenCongTy = reader.GetString(8);
                tv.SoDienThoai = reader.GetString(9);
                tv.DiaChiCongTy = reader.GetString(10);
                tv.NamThanhLap = reader.GetInt32(11);
                tv.Website = reader.GetString(12);
                tv.QuyMo = reader.GetString(13);
                tv.DiaChiNhaMay = reader.GetString(14);
                tv.QuyMoNhaMay = reader.GetString(15);
                tv.HinhAnh = reader.GetString(16);
                tv.GioiThieuVeCongTy = reader.GetString(17);
                tv.MaLoaiThanhVien = reader.GetInt32(18);
                tv.TenLoaiThanhVien = reader.GetString(19);
                tv.TinhTrangThanhVien = reader.GetInt32(20);
                tv.LaThanhVien = reader.GetBoolean(21);
                tv.NgayBatDau = reader.GetDateTime(22);
                tv.NgayKetThuc = reader.GetDateTime(23);
                list_ThanhVien.Add(tv);
            }
            conn.Close();
            return list_ThanhVien;
        }

        public int TongThanhVien()
        {
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_TONGTHANHVIEN", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            int totalRow = int.Parse(cmd.ExecuteScalar().ToString());
            conn.Close();
            return totalRow;
        }

        public int CapNhatTinhTrangThanhVien(int id, int tinhtrang)
        {
            int matv = id;
            ThanhVien tv = LayThanhVienTheoMa(matv);
            tv.TinhTrangThanhVien = tinhtrang;
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("CAPNHATTINHTRANGTHANHVIEN", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaTV", matv);
            cmd.Parameters.AddWithValue("@LoaiThanhVien", tv.maLoaiThanhVien);
            cmd.Parameters.AddWithValue("@TinhTrangThanhVien", tv.TinhTrangThanhVien);
            cmd.Parameters.AddWithValue("@LaThanhVien", tv.LaThanhVien);
            conn.Open();
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            return kq;
        }

        public ThanhVien LayThanhVienTheoMa(int MaTV)
        {
            ThanhVien tv = new ThanhVien();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_LAYTHANHVIENTHEOMA", conn);
            cmd.Parameters.AddWithValue("@MATV", MaTV);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tv.MaTV = reader.GetInt32(0);
                tv.Email = reader.GetString(1);
                tv.Password = reader.GetString(2);
                tv.MaQuocGia = reader.GetInt32(3);
                tv.TenQuocGia = reader.GetString(4);
                tv.maTinhThanh_Bang = reader.GetInt32(5);
                tv.TenTinhThanh_Bang = reader.GetString(6);
                tv.HoTen = reader.GetString(7);
                tv.TenCongTy = reader.GetString(8);
                tv.SoDienThoai = reader.GetString(9);
                tv.DiaChiCongTy = reader.GetString(10);
                tv.NamThanhLap = reader.GetInt32(11);
                tv.Website = reader.GetString(12);
                tv.QuyMo = reader.GetString(13);
                tv.DiaChiNhaMay = reader.GetString(14);
                tv.QuyMoNhaMay = reader.GetString(15);
                tv.HinhAnh = reader.GetString(16);
                tv.GioiThieuVeCongTy = reader.GetString(17);
                tv.MaLoaiThanhVien = reader.GetInt32(18);
                tv.TenLoaiThanhVien = reader.GetString(19);
                tv.TinhTrangThanhVien = reader.GetInt32(20);
                tv.LaThanhVien = reader.GetBoolean(21);
                tv.NgayBatDau = reader.GetDateTime(22);
                tv.NgayKetThuc = reader.GetDateTime(23);
            }
            conn.Close();
            return tv;
        }

        public ArrayList DanhSachThanhVienKhongPhanTrang()
        {
            ArrayList list_ThanhVien = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHTHANHVIENKHONGPHANTRANG", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ThanhVien tv = new ThanhVien();
                tv.MaTV = reader.GetInt32(0);
                tv.HoTen = reader.GetString(1);
                list_ThanhVien.Add(tv);
            }
            conn.Close();
            return list_ThanhVien;
        }
    }
}